/**
	Constructs a 

	@author Francis Villanueva
	@version 2006-10-17
	@copyright (c) 2006 The Chopping Block, Inc. All rights reserved.

	@usage

	- Activate with:
	<code>

	</code>

*/

import DelegateArgs;
import VideoPlayerController;

//include Timber data classes for XML
import timber.data.XMLData;

class VideoPlayer {
	
	//set variables for each piece of MVC
 	private var player_controller:VideoPlayerController;
	
	//load config xml
	private var configData:XMLData;

	private var url:String;
	private var callback:Function;
	
	private var title:String;
	private var thumbnailbase:String;
	private var largebase:String;
	
	// ===========================================================
	// - CONSTRUCTOR 
	// ===========================================================
	function VideoPlayer (target:MovieClip, configXML:String) {
		trace("Video Player constructed");
		load(configXML, loadSuccess);
	}

	// ===========================================================
	// - LOAD CONFIG XML DATA
	// ===========================================================
	function load (url:String, callback:Function){
		trace("load");
	
		//if test through flash
		this.url = url;
		//if in a browser
		//this.url = url + "?id="+Math.floor(Math.random()*100000);
		this.callback = callback;
		//parse config xml
		configData = new XMLData(this.url, DelegateArgs.create(this, callback));
	}

	function loadSuccess (val:Boolean){
		if (val) {
			var msg:String = "Config data success!"; trace(msg);
			init ();
		} else {
			//display error message
			var msg:String = "Error loading configuration file.";
			trace(msg);
			displayError(msg);
		}
	}

	// ===========================================================
	// - INITIALIZE PLAYER
	// ===========================================================

	function init (){
		trace("init");
		var settings = configData["config"];
		// Create the controller. (pass config data)
		player_controller = new VideoPlayerController(settings);
		//empty settings
		settings = null;
	};

	// ===========================================================
	// - ERROR HANDLING
	// ===========================================================

	function displayError(message:String) {
		trace("displayError");
	}
	
	public static function start (target:MovieClip, url:String) {
		var videoPlayer:VideoPlayer = new VideoPlayer(target, url);
	}

}